﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web;
using Microsoft.Win32;
using CommonLib;

namespace DataAccess.ODBC
{
    public class ODBCDSN
    {
        /// <summary>
        /// sDatabase='CSSTD' 表示連公用資料庫
        /// </summary>
        /// <param name="sDatabase"></param>
        /// <returns></returns>
        public static string SqlConn(string sDatabase)
        {

            return string.Format("server={0};uid={1};pwd={2};Database={3};",
                       getRegistry("SqlServerName"), getRegistry("SqlUserName"), EncryptionLib.GetDecryptor(getRegistry("SqlPassword") == null ? "" : getRegistry("SqlPassword").ToString(),true), sDatabase);


        }
        public static string WwsPubDb
        {
            get
            {
                //先判斷使用的資料庫的類型


                string connStr = string.Empty;

                object dbType = getRegistry("DatabaseType");

                if (null != dbType)
                {
                    switch (dbType.ToString())
                    {
                        case "Interbase":
                            connStr = string.Format(@"DSN=WwsPubDb;DataBase={0}:{1}Csstd.GDB;UID=SYSDBA;PWD=masterkey",
                                         getRegistry("IbServerName"), getRegistry("IbDatabasePath"));
                            break;
                        case "SQL Server":
                            connStr = string.Format(@"Driver={{SQL Server}};Server={0};uid={1};pwd={2};database=CSSTD",
                                getRegistry("SqlServerName"), getRegistry("SqlUserName"), EncryptionLib.GetDecryptor(getRegistry("SqlPassword") == null ? "" : getRegistry("SqlPassword").ToString(),true));
                            break;
                    }

                }

                return connStr;


                //if (ConfigurationManager.AppSettings["WwsPubDb"] != null)
                //{
                //    return ConfigurationManager.AppSettings["WwsPubDb"].ToString();
                //}
                //else
                //{
                //    return string.Empty;
                //}

            }
        }
        public static string WwsCustDb
        {
            get
            {

                string connStr = string.Empty;

                object dbType = getRegistry("DatabaseType");

                if (null != dbType)
                {
                    switch (dbType.ToString())
                    {
                        case "Interbase":
                            connStr = string.Format(@"DSN=WwsCustDb;DataBase={0}:{1}DAT\{2}.GDB;UID=SYSDBA;PWD=masterkey",
                                         getRegistry("IbServerName"), getRegistry("IbDatabasePath"), HttpContext.Current.Session["EnterPrise"]);
                            break;
                        case "SQL Server":
                            connStr = string.Format(@"Driver={{SQL Server}};Server={0};uid={1};pwd={2};database={3}",
                                getRegistry("SqlServerName"), getRegistry("SqlUserName"), EncryptionLib.GetDecryptor(getRegistry("SqlPassword") == null ? "" : getRegistry("SqlPassword").ToString(),true), HttpContext.Current.Session["EnterPrise"]);
                            break;
                    }

                }

                return connStr;

                //if (ConfigurationManager.AppSettings["WwsCustDb"] != null && HttpContext.Current.Session["EnterPrise"]!=null)
                //{

                //    return string.Format(ConfigurationManager.AppSettings["WwsCustDb"].ToString(),HttpContext.Current.Session["EnterPrise"].ToString());
                //}
                //else
                //{
                //    return string.Empty;
                //}

            }
        }

        /// <summary>
        /// 獲取HRS相關注冊表信息(須引入 Microsoft.Win32)
        /// </summary>
        /// <param name="strName">鍵</param>
        /// <returns>鍵值</returns>
        public static object getRegistry(string strName)
        {
            RegistryKey rKey = Registry.LocalMachine;
            RegistryKey rKey2 = rKey.CreateSubKey("SOFTWARE\\WINTON\\WMIS\\AppInfo");

            return rKey2.GetValue(strName);
        }



    }
}
